package com.kingdee.jdy.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingdee.jdy.domain.AccountInfo;
import java.util.HashMap;

/**
 * 账套信息工具类
 */
public class AccountInfoUtil {

    public static AccountInfo GetAccountInfo(String access_token) throws Exception {
        HashMap<String, Object> urlParam = new HashMap<>();
        urlParam.put("access_token", access_token);

        JSONObject res = HttpUtil.doPost("https://api.kingdee.com/jdy/sys/accountGroup", urlParam, null, null);
        if (res != null) {
            String errorCode = res.getString("errorCode");
            if (errorCode.equalsIgnoreCase("success")) {
                JSONArray data = res.getJSONArray("data");
                if (data != null && data.size() > 0) {
                    JSONArray accountGroups = data.getJSONObject(0).getJSONArray("accountGroups");
                    if (accountGroups != null && accountGroups.size() >0 ) {
                        for (int i = 0; i < accountGroups.size(); i++) {
                            JSONObject accountGroup = accountGroups.getJSONObject(i);
                            AccountInfo accountInfo = accountGroup.toJavaObject(AccountInfo.class);
                            if (accountInfo.getAccountName().equalsIgnoreCase("金蝶软件（中国）有限公司")){
                                return accountInfo;
                            }
                        }
                    }
                }

            }
        }

        return null;
    }

}
